package com.cj.rxdo;

import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.alivc.player.AliVcMediaPlayer;
import com.alivc.player.MediaPlayer;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class ChatActivity extends AppCompatActivity {
    @BindView(R.id.fl)FrameLayout fl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        initVideo();
    }

    private AliVcMediaPlayer player;
    private SurfaceView surfaceView;
    private void initVideo() {
        surfaceView=new SurfaceView(this);
        fl.removeAllViews();
        fl.addView(surfaceView);
        //创建播放器
        player=new AliVcMediaPlayer(this,surfaceView);
        //注册监听器
        player.setPreparedListener(new PrepareListener()); //预备
        player.setErrorListener(new ErrorListener());  //错误处理
        player.setInfoListener(new VideoInfolistener());  //播放信息事件通知中
        player.setSeekCompleteListener(new VideoSeekCompletelistener());
        player.setCompletedListener(new VideoCompletelistener());
        player.setVideoSizeChangeListener(new VideoSizeChangelistener());
        player.setBufferingUpdateListener(new VideoBufferUpdatelistener());

        player.setDefaultDecoder(0);//0:硬件解码，1软解
        player.seekTo(0);//从某处开始播放
        player.prepareToPlay("http://iiitestin.oss-cn-hangzhou.aliyuncs.com/m3u8SegementIn74/ssss.mp4");
        Observable.timer(5000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        player.play();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Long aLong) {

                    }
                });

    }


    private class PrepareListener implements AliVcMediaPlayer.MediaPlayerPreparedListener{

        @Override
        public void onPrepared() {

        }
    }

    private class ErrorListener implements AliVcMediaPlayer.MediaPlayerErrorListener{

        @Override
        public void onError(int what, int i1) {
            switch(what)
            {
                case MediaPlayer.ALIVC_ERR_ILLEGALSTATUS:
                    Toast.makeText(ChatActivity.this,"非法状态",Toast.LENGTH_SHORT).show();
                    // 非法状态！
                    break;
                case MediaPlayer.ALIVC_ERR_NO_NETWORK:
                    //report_error("视频资源或网络不可用！", true);
                    Toast.makeText(ChatActivity.this,"视频资源或网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case MediaPlayer.ALIVC_ERR_INVALID_INPUTFILE:
                    Toast.makeText(ChatActivity.this,"invalid_inputfile",Toast.LENGTH_SHORT).show();
                    //视频资源或网络不可用！
                    break;
                case MediaPlayer.ALIVC_ERR_NO_SUPPORT_CODEC:
                    Toast.makeText(ChatActivity.this,"无支持的解码器",Toast.LENGTH_SHORT).show();
                    //无支持的解码器!
                    break;
//                case MediaPlayer.ALIVC_ERR_FUNCTION_DENY:
//                    //无此操作权限!
//                    break;
                case MediaPlayer.ALIVC_ERR_UNKNOWN:
                    Toast.makeText(ChatActivity.this,"未知错误",Toast.LENGTH_SHORT).show();
                    //未知错误!
                    break;
                case MediaPlayer.ALIVC_ERR_NOTAUTH:
                    Toast.makeText(ChatActivity.this,"未鉴权",Toast.LENGTH_SHORT).show();
                    //未鉴权!
                    break;
                case MediaPlayer.ALIVC_ERR_READD:
                    Toast.makeText(ChatActivity.this,"资源访问失败",Toast.LENGTH_SHORT).show();
                    //资源访问失败!
                    break;
                default:
                    //播放器错误!
                    Toast.makeText(ChatActivity.this,"播放器错误!",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private class VideoInfolistener implements AliVcMediaPlayer.MediaPlayerInfoListener {
        public void onInfo(int what, int extra){
            switch (what)
            {
                case MediaPlayer.MEDIA_INFO_UNKNOW:
                    Toast.makeText(ChatActivity.this,"未知",Toast.LENGTH_SHORT).show();
                    // 未知
                    break;
                case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                    Toast.makeText(ChatActivity.this,"开始缓冲",Toast.LENGTH_SHORT).show();
                    // 开始缓冲
                    break;
                case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                    Toast.makeText(ChatActivity.this,"结束缓冲",Toast.LENGTH_SHORT).show();
                    // 结束缓冲
                    break;
                case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                    // 首帧显示时间
                    Toast.makeText(ChatActivity.this,"首帧显示",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }


    private class VideoSeekCompletelistener implements MediaPlayer.MediaPlayerSeekCompleteListener {
        @Override
        public void onSeekCompleted() {
//            Toast.makeText(ChatActivity.this,"onSeekCompleted",Toast.LENGTH_SHORT).show();
        }
    }

    private class VideoCompletelistener implements MediaPlayer.MediaPlayerCompletedListener {
        @Override
        public void onCompleted() {
//            Toast.makeText(ChatActivity.this,"video completed",Toast.LENGTH_SHORT).show();
        }
    }

    private class VideoSizeChangelistener implements MediaPlayer.MediaPlayerVideoSizeChangeListener {
        @Override
        public void onVideoSizeChange(int i, int i1) {
            Toast.makeText(ChatActivity.this,"onVideoSizeChange",Toast.LENGTH_SHORT).show();
        }
    }

    private class VideoBufferUpdatelistener implements MediaPlayer.MediaPlayerBufferingUpdateListener {
        @Override
        public void onBufferingUpdateListener(int i) {
            Toast.makeText(ChatActivity.this,"onBufferingUpdateListener",Toast.LENGTH_SHORT).show();
        }
    }
}
